from flask import Flask, request
from flask_restful import Resource, Api, marshal_with, fields, reqparse, request
from tinystack.nova import NovaFlavor
from tinystack.glance import Glance


app = Flask(__name__)
api = Api(app)


class Image(Resource):

    def get(self):
        glance = Glance()
        images = glance.list()
        return images

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("", type=int)
        args = parser.parse_args()

        glance = Glance()
        glance.add()

    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id")
        args = parser.parse_args()

        glance = Glance()
        image_uuid = glance.search(args.id)
        glance.delete(image_uuid)


class Flavor(Resource):
    def get(self):
        nova = NovaFlavor()
        flavors = nova.list()
        return flavors

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("vcpu", type=int)
        parser.add_argument("memory", type=int)
        parser.add_argument("root", type=int)
        parser.add_argument("name")
        args = parser.parse_args()

        nova = NovaFlavor()
        nova.add(**{
            "vcpu": args.vcpu,
            "memory": args.memory,
            "root": args.root,
            "name": args.name
        })

    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id")
        args = parser.parse_args()

        nova = NovaFlavor()
        flavor_uuid = nova.search(args.id)
        nova.delete(flavor_uuid)

api.add_resource(Image, "/images")
api.add_resource(Flavor, "/flavors")